using System;
using System.Threading;
using Gtk;
using Gdk;
using Mono.WebBrowser;
using WebKit;
using System.Xml;
using System.IO;

public partial class MainWindow: Gtk.Window
{	
	WebView webview1 = new WebView();
	WebView webview2 = new WebView();
	WebView webview3 = new WebView();
	WebView webview4 = new WebView();
	string movieurl = "http://www.youtube.com/v/WSt7NTFbWZw?autoplay=1&amp;f=videos&amp;app=youtube_gdata";
	int interval = 1;
	string plid = "0";
	bool ticking = true;
	bool reloading = false;
	int n = 0;	
	
	public MainWindow (): base (Gtk.WindowType.Toplevel)
	{
		Build ();
		
		
		
		
		webview1.WidthRequest = 480;
		webview1.HeightRequest = 290;
		hbox3.Add(webview1);
		webview1.Show(); 
		scrolledwindow2.Add(webview2);
		webview2.Show();
		vbox3.Add(webview3);
		webview3.Show();
		scrolledwindow1.Add(webview4);
		webview4.Show();
		webview1.LoadFinished += delegate {
			n = 0;
			if(reloading){
			    Thread t = new Thread(new ThreadStart(MovieTimer));
			    t.Start();
			}else{
				reloading = false;
			}
		};
		webview2.Open("http://blooming-sunset-7188.heroku.com/");
		webview3.Open("http://blooming-sunset-7188.heroku.com/about");
	    
		MovieTimerTicked();
		
	}
	
	protected void OnDeleteEvent (object sender, DeleteEventArgs a)
	{
		ticking = false;
		interval = 0;
		Gtk.Application.Quit ();
		a.RetVal = true;
	}

	protected void OnActionActivated (object sender, System.EventArgs e)
	{
		
	}

	protected void OnAction9Activated (object sender, System.EventArgs e)
	{
		
	}
	protected void OnAction1Activated (object sender, System.EventArgs e)
	{
		
	}
	protected void OnAction5Activated (object sender, System.EventArgs e)
	{
		Console.WriteLine("skip");
		interval = 0;
	}

	protected void OnAction4Activated (object sender, System.EventArgs e)
	{
		Console.WriteLine("exit");
		ticking = false;
		interval = 0;
		Main.Quit();
	}

	protected void OnButton1Clicked (object sender, System.EventArgs e)
	{
		string query = entry1.Text;
		Console.WriteLine("検索:" + query);
		webview4.Open("http://linsgamecircle.appspot.com/?q=" + query);
	}

	protected void OnDefaultActivated (object sender, System.EventArgs e)
	{
		
	}
	public void MovieTimer(){
	  while(n < interval){
		  Thread.Sleep(1000);
		 	n ++;
	    }
		if(ticking){
		    MovieTimerTicked();
		}
	}
	public void MovieTimerTicked(){
		Console.WriteLine("interval 、movieurlの再設定とブラウザロードの後の新規スレッドスタート");
		//xml読み込み
		String URLString = "http://blooming-sunset-7188.heroku.com/next?b=" + plid;
		XmlTextReader reader = new XmlTextReader (URLString);
		int flag = 0;
		bool fff = false;
		while (reader.Read()) {
			switch (reader.NodeType) {
        case XmlNodeType.Element: // ノードはエレメントです。
					if(reader.Name == "id"){
					       fff = true;
							flag = 1;
						}else if(reader.Name == "url"){
							flag = 2;
						}else if(reader.Name == "sec"){
							flag = 3;
						}
            			break;
	
        			case XmlNodeType.Text: // 各エレメントのテキストを表示します。
            			switch(flag){
            				case 1:
            					plid = reader.Value;
            					flag = 0;
            					break;
            				case 2:
            					movieurl = reader.Value.Replace("version=3","autoplay=1");
            					flag = 0;
            					break;
            				case 3:
            					interval = int.Parse(reader.Value) + 2;
            					flag = 0;
            					break;
            			}
            			
            			break;

        			case XmlNodeType. EndElement: // エレメントの最後を表示します。
            			Console.Write("</" + reader.Name);
          				Console.WriteLine(">");
            			break;
 				}
			}
		if (fff == false){
			movieurl = "http://www.youtube.com/v/0uWylMT3WyI&hl=en_US&feature=player_embedded&autoplay=1";
			interval = 219;
		}
		webview2.Reload();
		webview1.Open(movieurl);
	}

	protected void OnButton2Activated (object sender, System.EventArgs e)
	{
		throw new System.NotImplementedException ();
	}

	protected void OnButton2Clicked (object sender, System.EventArgs e)
	{
		WebView wv = new WebView();
		wv.Open("http://blooming-sunset-7188.heroku.com/direct?v=" + entry2.Text); 
	}

	protected void OnAction8Activated (object sender, System.EventArgs e)
	{
		n = 0;
		reloading = true;
		webview1.Reload();
	}

}
